﻿Public Class FormInvoice

    Private Sub FormInvoice_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        UI.FormatDataGrid(Me.dgv)

        Me.txtPurchaseNo.Text = NoGenerator.GenerateNewPurchaseNo()

        Me.cboStore.DisplayMember = "StoreName"
        Me.cboStore.ValueMember = "StoreId"
        Me.cboStore.DataSource = Cmd.ExecuteDataTable("SELECT * FROM Store WHERE Active=1")

        Me.cboVendor.DisplayMember = "Vendor"
        Me.cboVendor.ValueMember = "VendorId"
        Me.cboVendor.DataSource = Cmd.ExecuteDataTable("SELECT VendorId,Vendor = VendorName+' ('+Phone+')' FROM Vendor WHERE Active=1")

    End Sub
    Private Sub ClearLine()
        Me.txtProductId.Text = ""
        Me.txtProductName.Text = ""
        Me.txtQty.Text = ""
        Me.txtPrice.Text = ""
        Me.txtAmount.Text = ""
        Me.txtProductCode.Text = ""

        Me.txtProductCode.Focus()

    End Sub
    Private Sub BrowseProduct()
        Dim frm As New FormProductBrowser()
        frm.txtSearch.Text = Me.txtProductCode.Text
        If frm.ShowDialog() = Windows.Forms.DialogResult.OK Then
            Dim row = frm.dgv.SelectedRows(0)
            Me.txtProductId.Text = row.Cells("ProductId").Value
            Me.txtProductCode.Text = row.Cells("ProductCode").Value
            Me.txtProductName.Text = row.Cells("ProductName").Value
            Me.txtPrice.Text = row.Cells("Price").Value
            Me.txtQty.Text = "1"
            Me.txtAmount.Text = Me.txtPrice.Text
        End If
    End Sub
    Private Function FindProductInList(ByVal id As Int16) As DataGridViewRow
        For Each row As DataGridViewRow In Me.dgv.Rows
            If row.Cells("ProductId").Value = id Then
                Return row
            End If
        Next
        Return Nothing
    End Function
    Private Sub btnBrown_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBrown.Click
        BrowseProduct()
    End Sub

    Private Sub txtProductCode_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtProductCode.KeyDown
        If e.KeyCode <> Keys.Enter Then
            Exit Sub
        End If


        Cmd.Parameters("@Code") = Me.txtProductCode.Text
        Dim dt = Cmd.ExecuteDataTable("SELECT * FROM Product WHERE ProductCode=@Code")

        If (dt.Rows.Count = 0) Then
            ClearLine()
            BrowseProduct()
            Exit Sub
        End If

        Dim row = dt.Rows(0)
        Me.txtProductId.Text = row("ProductId")
        Me.txtProductName.Text = row("ProductName")
        Me.txtPrice.Text = row("Price")
        Me.txtQty.Text = 1
        Me.txtAmount.Text = txtPrice.Text
    End Sub

    Private Sub btnAddLine_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddLine.Click
        '' Me.dgv.Rows.Add(Me.txtProductId.Text, Me.txtProductCode.Text, Me.txtProductName.Text, Me.txtPrice.Text, Me.txtQty.Text, Me.txtAmount.Text)
        If txtProductCode.Text.Trim() = "" Then
            MsgBox("Please Input Code product !!!")
            txtProductCode.Focus()
            Exit Sub
        End If
        Dim searchRow = FindProductInList(Me.txtProductId.Text)

        If searchRow Is Nothing Then
            Dim i = Me.dgv.Rows.Add()
            Dim row = Me.dgv.Rows(i)
            row.Cells("ProductId").Value = Me.txtProductId.Text
            row.Cells("ProductCode").Value = Me.txtProductCode.Text
            row.Cells("ProductName").Value = Me.txtProductName.Text
            row.Cells("Price").Value = Val(Me.txtPrice.Text)
            row.Cells("Qty").Value = Val(Me.txtQty.Text)
            row.Cells("Amount").Value = Val(Me.txtAmount.Text)
        Else
            searchRow.Cells("Qty").Value = searchRow.Cells("Qty").Value + Val(Me.txtQty.Text)
            searchRow.Cells("Price").Value = Me.txtPrice.Text
            searchRow.Cells("Amount").Value = Val(searchRow.Cells("Qty").Value) * Me.txtPrice.Text
        End If

        Dim price As Double = 0
        Dim subt As Double = 0
        For i As Integer = 0 To dgv.RowCount - 1
            Dim row = Me.dgv.Rows(i)

            price = Val(row.Cells("Amount").Value)
            subt += price
        Next i
        txtSubTotal.Text = subt

        ClearLine()
    End Sub

    Private Sub Label16_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label16.Click

    End Sub

    Private Sub txtDiscount_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtDiscount.TextChanged
        If IsNumeric(txtDiscount.Text) Then
            If Val(txtDiscount.Text) > 100 Or Val(txtDiscount.Text) < 0 Then
                MsgBox("Discount must be Less than 100 and bigger than 0....!")
            Else
                txtTotal.Text = Val(txtSubTotal.Text) - (Val(txtSubTotal.Text) * Val(txtDiscount.Text)) / 100
            End If
        Else
            MsgBox("You Must Input only Numerice......!")

        End If
    End Sub
End Class